<?php
require_once("mysqlUser/mysql.php");

require_once("user.php");
require_once("robot.php");
require_once("mainWeapon.php");
require_once("secWeapon.php");

class storeHouse
{
	private $uid;
	private $user;
	
	public $robotParts;
	public $mainWeaponParts;
	public $secWeaponParts;
	
	private $equipRobotID=-1;
	
	/**
	 * 玩家的机械库
	 * 可买卖设备，改变装备
	 * @param mixed $uid 玩家uid
	 */
	public function storeHouse($uid)
	{
		$this->uid = $uid;
		
		$this->user = new user($uid);
		
		$this->getRobots();
		$this->getMainWeapons();
		$this->getSecWeapons();
	}
	/**
	* 买装备
	* @param mixed $type 1:机甲，2：主武器，3：副武器
	* @param mixed $id 要买的装备编号
	* @param mixed $inc 要买的装备的强化等级
	* @return 成功返回1，失败返回-1 
	*/	
	public function buy($type,$id,$inc)
	{
		$part;
		switch ($type)
		{
			case 1:
				$part = new robotBase($id,$inc);
				break;  
			case 2:
				$part = new mainWeaponBase($id,$inc);
				break;
			case 3:
				$part = new secWeaponBase($id,$inc);
				break;	
			default:
				break;
		}
		
		$cost = $part->cost;
		//玩家的钱大于设备的价格才能购买
		if($cost <= $this->user->money)
		{
			switch ($type)
			{
				case 1:
					robot::giveToUser($this->uid,$id,$inc);
					break;  
				case 2:
					mainWeapon::giveToUser($this->uid,$id,$inc);
					break;
				case 3:
					secWeapon::giveToUser($this->uid,$id,$inc);
					break;	
				default:
					break;
			}
			$this->user->money -= $cost;
			return 1;
				
		}else return -1;
	}
	
	/**
	 * 卖装备
	 * @param mixed $type 1:机甲，2：主武器，3：副武器
	 * @param mixed $id 要卖掉第几个
	 * @return 成功返回卖出物品的pid，失败返回-1 
	 */	
	public function sell($type,$id)//如果是武器，还没有将武器从机甲上卸载
	{
		$parts;
		switch ($type)
		{
			case 0:
				$parts = &$this->robotParts;
				if($parts[$id]->equip==1)return -1;//已装备的机甲是不能卖的
				break;  
			case 1:
				$parts = &$this->mainWeaponParts;
				break;
			case 2:
				$parts = &$this->secWeaponParts;
				break;	
			default:
				break;
		}
		
		//查出商品价格
		$pid = $parts[$id]->pid;
		$cost = $parts[$id]->baseValues->cost;
		//卖东西要色本
		$cost *= 0.6 ;
		//耐久越少色得越凶
		$d =  $parts[$id]->dur/$parts[$id]->baseValues->maxdur;
		//如果是机甲，额，还要看机甲现在有多少hp了
		if($type==0)
		{
			$h = $parts[$id]->hp/$parts[$id]->baseValues->maxhp;
			$d *= $h;
		}
		//总之这就是算出来值多少钱了
		$cost *= $d;
			
		//给玩家加上卖掉物品的钱
		$this->user->money += floor($cost);
			
		//删除该物品
		$parts[$id]->drop();
		array_splice($parts,$id,1);
		
		//如果玩家有机甲装备了卖掉的武器，则将此机甲的相应武器接口清空
		if($type==1)
		{
			foreach($this->robotParts as $rob)
			{
				$cons = $rob->getWep_m();
				for($i=0;$i<count($cons);$i++)
				{
					if($cons[$i]==$pid)$cons[$i]="0";
				}
				$rob->setWep_m($cons);
			}
		}elseif($type==2)
		{
			foreach($this->robotParts as $rob)
			{
				$cons = $rob->getWep_s();
				for($i=0;$i<count($cons);$i++)
				{
					if($cons[$i]==$pid)$cons[$i]="0";
				}
				$rob->setWep_s($cons);
			}
		}
		
		return $pid;
	}
	
	/**
	 * 装备机甲
	 * 
	 * @param mixed $id 将第几个机甲设置为当前装备
	 * @return 成功返回1，失败返回-1 
	 */	
	public function equipRobot($id)
	{
		//如果设备的耐久大于0并且玩家够等级了才能装备
		if($this->robotParts[$id]->dur > 0 && $this->user->level >= $this->robotParts[$id]->baseValues->level)
		{
			foreach($this->robotParts as $robot)$robot->equip = 0;
			$this->robotParts[$id]->equip=1;
			$this->equipRobotID = $id;
				
			return 1;
		}else return -1;	
	}
	
	/**
	 * 一次性更新所有机甲的武器装备
	 * 现在是直接处理从客户端发来的数据，没有经过验证
	 *
	 * @param mixed $wepData_m 主武器接口的武器
	 * @param mixed $wepData_s 副武器接口的武器
	 */	
	public function updateWeapon($wepData_m,$wepData_s)
	{
		$count = count($this->robotParts);
		for($i=0;$i<$count;$i+=1)
		{
			$wep_m = $wepData_m[$i];
			$wep_s = $wepData_s[$i];
			$this->robotParts[$i]->setWep_m($wep_m);
			$this->robotParts[$i]->setWep_s($wep_s);
		}
	}
	
	/**
	 * 给机甲装备主武器(现在是一次性更新，这个没用上)
	 *
	 * @param mixed $id 玩家拥有的第几个主武器
	 * @param mixed $conID 武器挂接点的编号
	 * @return 成功返回1，失败返回-1
	 */	
	public function installMainWeapon($id,$conID)
	{
		$robot = $this->robotParts[$this->equipRobotID];
		
		$cons = explode(",",$robot->wep_m);
		
		if($cons[$conID]=="0")//如果当前挂接点没有武器才能继续
		{
			$weapon = $this->mainWeaponParts[$id];
			
			if($robot->baseValues->level >= $weapon->baseValues->level && $weapon->dur>0)//如果机甲等级够了，并且武器耐久大于0才能装备
			{
				$cons[$conID]=$weapon->pid;
				$robot->setWep_m($cons);
				return 1;
			}return -1;
		}return -1;
	}
	/**
	 * 卸载机甲某个主武器挂接点的武器(现在是一次性更新，这个没用上)
	 *
	 * @param mixed $conID 武器挂接点的编号
	 * @return  成功返回1，失败返回-1
	 */	
	public function uninstallMainWeapon($conID)
	{
		$robot = $this->robotParts[$this->equipRobotID];
		$cons = explode(",",$robot->wep_m);
		
		if($cons[$conID]!="0")
		{
			$cons[$conID]="0";
			$robot->setWep_m($cons);
			return 1;
		}return -1;
	}
	
	/**
	* 给机甲装备副武器(现在是一次性更新，这个没用上)
	*
	* @param mixed $id 玩家拥有的第几个副武器
	* @param mixed $conID 武器挂接点的编号
	* @return 成功返回1，失败返回-1
	*/	
	public function installSecWeapon($id,$conID)
	{
		$robot = $this->robotParts[$this->equipRobotID];
		
		$cons = explode(",",$robot->wep_s);
		
		if($cons[$conID]=="0")//如果当前挂接点没有武器才能继续
		{
			$weapon = $this->secWeaponParts[$id];
			
			if($robot->baseValues->level >= $weapon->baseValues->level && $weapon->dur>0)//如果机甲等级够了，并且武器耐久大于0才能装备
			{
				$cons[$conID]=$weapon->pid;
				
				$robot->setWep_s($cons);
				
				return 1;
			}return -1;
		}return -1;
	}

	/**
	* 卸载机甲某个副武器挂接点的武器(现在是一次性更新，这个没用上)
	*
	* @param mixed $conID 武器挂接点的编号
	* @return  成功返回1，失败返回-1
	*/	
	public function uninstallSecWeapon($conID)
	{
		$robot = $this->robotParts[$this->equipRobotID];
		$cons = explode(",",$robot->wep_s);
		
		if($cons[$conID]!="0")
		{
			$cons[$conID]="0";
			$robot->setWep_s($cons);
			
			return 1;
		}return -1;
	}
	///////////////////////////////////////
	public function getCurrentEquip()
	{
		$command = "select * from ".robot::$table." where uid={$this->uid}";
		$result = mysql::run($command);
		
		$robot;
		while($vals=mysql_fetch_array($result))
		{
			$part = new robot($vals["pid"]);
			if($part->equip==1)
			{
				$robot = &$part;
				break;
			}
		}
		
		return array("pid"=>$robot->pid,"wep_m"=>$robot->wep_m,"wep_s"=>$robot->wep_s);
	}
	
	
	/**
	 * 取得玩家拥有的机甲
	 */	
	public function getRobots()
	{
		$command = "select * from ".robot::$table." where uid={$this->uid}";
		$result = mysql::run($command);
		
		$cot=-1;
		$data;
		while($vals=mysql_fetch_array($result))
		{
			$cot+=1;
			
			$part = new robot($vals["pid"]);
			
			$this->robotParts[]=$part;
			
			if($part->equip==1)$this->equipRobotID = $cot;
			
			$base = new robotBase($part->id,$part->inc);
			
			$data[] = array("id"=>$part->id,"inc"=>$part->inc,"name"=>$base->name,"equip"=>$part->equip,"wep_m"=>$part->wep_m,"wep_s"=>$part->wep_s,"hp"=>$part->hp,"dur"=>$part->dur);
		}
		
		return $data;
	}
	
	/**
	 * 取得玩家拥有的主武器
	 */	
	public function getMainWeapons()
	{
		$command = "select * from ".mainWeapon::$table." where uid=$this->uid";
		$result = mysql::run($command);
		
		$data;
		while($vals=mysql_fetch_array($result))
		{
			$part = new mainWeapon($vals["pid"]);
			$this->mainWeaponParts[]=$part;
			
			$base = new mainWeaponBase($part->id,$part->inc);
			$data[] = array("pid"=>$vals["pid"],"id"=>$part->id,"inc"=>$part->inc,"name"=>$base->name,"dur"=>$part->dur);
		}
		return $data;;
	}
	/**
	* 取得玩家拥有的副武器
	*/	
	public function getSecWeapons()
	{
		$command = "select * from ".secWeapon::$table." where uid=$this->uid";
		$result = mysql::run($command);
		
		$data;
		while($vals=mysql_fetch_array($result))
		{
			$part = new secWeapon($vals["pid"]);
			$this->secWeaponParts[]=$part;
			
			$base = new secWeaponBase($part->id,$part->inc);
			
			$data[] = array("pid"=>$vals["pid"],"id"=>$part->id,"inc"=>$part->inc,"name"=>$base->name,"dur"=>$part->dur);
		}
		return $data;;
	}
	
}

?>